/***************************************************************************
 *   Copyright (C) 2007 - 2009 by Andreas Theofilu                         *
 *   andreas@theosys.at                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation version 3 of the License.                *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef SPORTWATCHERWIDGET_H
#define SPORTWATCHERWIDGET_H

#include <qlabel.h>
#include <qdatetime.h>
#include <qpixmap.h>
#include <qimage.h>
#include <qpainter.h>
#include <qpen.h>
#include <QPoint>
#include <qrect.h>
#include <qstring.h>
#include <qtimer.h>
#include <KLocale>

#if defined HAVE_GDAL
   #include <gdal/gdal_priv.h>
#endif

#include "garmin.h"
#include "managefile.h"
#include "disassemble.h"

#include <QtGui/QWidget>
#include <QTreeWidget>
#include "ui_sportwatcherwidgetbase.h"

typedef enum {
	MPT_BMP = 0,
	MPT_SHP = 1,
	MPT_VRT = 2,
	MPT_GIF = 3,
	MPT_PNG = 4,
	MPT_SGI = 5,
	MPT_TIF = 6,
	MPT_WMS = 7,
	MPT_OSM = 8
} MPT;

typedef struct GEORECT
{
	double llat;
	double llon;
	double rlat;
	double rlon;
	int width;
	int height;
} GEORECT;

typedef struct AVGHEIGHT
{
	double alt;		// Altitude
	int    pos;		// Position in the chain, starting with 0
	struct AVGHEIGHT *prev;
	struct AVGHEIGHT *next;
} AVGHEIGHT;

//class sportwatcherWidget : public QWidget, public Ui::sportwatcherWidgetBase
class sportwatcherWidget : public QWidget, public Ui::sportwatcherWidgetBase
{
	Q_OBJECT

	public:
	   sportwatcherWidget(QWidget *parent = 0);
	   ~sportwatcherWidget();
	   /* $PUBLIC_FUNCTIONS$ */
	   void Initialize();

	private:
	   Ui::sportwatcherWidgetBase ui_sportwatcherWidgetBase;

	public slots:
	   /* $PUBLIC_SLOTS$ */
	   void btFullscreenSlot();
	   void btGlasMinusSlot();
	   void btGlasPlusSlot();
	   void btHandSlot();
	   void btGlasSlot();
	   void btFlagSlot();
	   void liLapsSlot(Q3ListViewItem *item);
	   void liActivitiesSlot(QTreeWidgetItem *item, int col);
	   void tabViewSlot(int tab);
	   void filePrint();
	   void fileSaveAs();
	   void fileSave();
	   void fileOpen();
	   void fileImport();
	   void fileNew();
	   void editRename();
	   void extrasSaveHR();
	   void extrasSettings();
	   void extrasWMSSettings();
	   void kcbCurveSlot(int idx);

	protected:
	   /* $PROTECTED_FUNCTIONS$ */
	   void destroy();
	   void showLaps();
	   void showTrack();
	   void showTrack(int zoom);
	   void showTrack(int zoom, const QRect &pan, LAP *lap);
	   void showCurves();
	   void showCurves(LAP *lap);
	   void showThreeCurve(int pw=0, int ph=0);
	   void resizeEvent(QResizeEvent *e);
	   void mouseMoveEvent(QMouseEvent *e);
	   void mousePressEvent(QMouseEvent *e);
	   void mouseReleaseEvent(QMouseEvent *e);

	protected:
	   QTimer timer;

	private:
	   void getActivities();
	   QDateTime *garmin_dtime (uint32 t);
	   bool writeTag(const QFile &fn, const QString &str, int indent);
	   double getAvgAlt (AVGHEIGHT *avgHeight, int pos);
	   AVGHEIGHT *getAvgPtr (AVGHEIGHT *avgHeight, int pos);
	   void saveGPX(const QString &fn);
	   void saveOSM(const QString &fn);
	   QTreeWidgetItem *findElement(QTreeWidget *wdg, const QString &val);
	   void drawGrHR (int x, int y);
	   void drawGrElev (int x, int y);
	   void drawGrSpeed (int x, int y);
	   qreal milToPixel(qreal, QPrinter &pr, bool dir=false);
#if defined HAVE_GDAL
	   bool writeWMSTag(double llat, double llon, double rlat, double rlon, int width, int height);
	   bool transCoords (double *x1, double *y1, double *x2, double *y2, int code, int width, int height, bool square);
	   QString *getProjection (int isrs, QString *srs);
	   bool warpImage(QString fn, QString *fName);
#endif

	private:
	   QWidget *mama;
	   QPixmap pmProfile, pmMap, pmHR, pmElevation, pmSpeed;
	   QPixmap pmPrMap, prHR, prElevation, prSpeed;	// These are used for printing
	   QDateTime StartTime;
	   QStringList files;
	   QRect mapPan;
	   KLocale *kl;		// This is for translating date and time to the locale
	   manageFile spw;
	   disassemble ds;
	   garmin_data *gmn;
	   int min_hr, max_hr, avg_hr;
	   int zfactor;
	   double min_height, max_height;
	   double min_speed, max_speed;
	   double total_distance;
	   double oldTransX, oldTransY;
	   unsigned long max_time;
	   bool stateHand, stateFlag, stateGlas;
	   int lmbPressed;
	   LAP *mapLap;
	   int curTab;
	   bool tabDirt0, tabDirt1, tabDirt2, tabDirt3;
	   bool ActivePrint;
	   QPainter printArea;
#if defined HAVE_GDAL
	   GDALDataset *poDataset;
	   int mFactor;
	   GEORECT geoRect;
#endif

	private:
	   /* Config Parameters */
	   int lower1, lower2, lower3;
	   int upper1, upper2, upper3;
	   int MaxHr, restHr;
	   int vo2max, weight, sampleTime;
	   int Units, MapType;
	   bool Serial, Contour, Forerunner;
	   bool DIRTY;
	   QString Device, Data, HRM, MAP;
};

#endif
